import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from "@/views/LoginView";
import AdminView from "@/views/AdminView";
import Layout from "@/views/Layout";
import RegisterView from "@/views/RegisterView";
import BookView from "@/views/BookView";
import TypeView from "@/views/TypeView";
import AuditView from "@/views/AuditView";
import HotelView from "@/views/HotelView";
import ReserveView from "@/views/ReserveView";
import LogView from "@/views/LogView";
import NoticeView from "@/views/NoticeView";

Vue.use(VueRouter)

const routes = [
    {
        path: '/login',
        name: 'login',
        component: LoginView
    },
    {
        path: '/register',
        name: 'Register',
        component: RegisterView
    },
    {
        path: '/',
        // name: 'Layout',
        component: Layout,
        children: [ // 子路由
            {
                path: '',
                name: 'home',
                component: HomeView
            },
            {
                path: 'admin',
                name: 'admin',
                component: AdminView
            },
            {
                path: 'book',
                name: 'book',
                component: BookView
            },
            {
                path: 'type',
                name: 'type',
                component: TypeView
            },
            {
                path: 'audit',
                name: 'audit',
                component: AuditView
            },
            {
                path: 'hotel',
                name: 'hotel',
                component: HotelView
            },
            {
                path: 'reserve',
                component: ReserveView
            },
            {
                path: 'log',
                component: LogView
            },
            {
                path: 'notice',
                component: NoticeView
            },
        ]
    },

]
const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
    const user = localStorage.getItem("user");
    if (!user && to.path !== '/login' && to.path !== '/register') {
        return next("/login");
    }
    next();
})

export default router
